package exception;

//自定义异常类
public class UserLogin {
    private String userName = "admin";
    private String password = "123456";
    //定义一个密码异常类，属于受查异常，即编译时出现的异常
    class PasswordException extends Exception{
        public PasswordException(String msg){
            super(msg);
        }
    }
    //定义一个用户名异常类，属于非受查异常，即运行时出现的异常
    class UserNameException extends RuntimeException{
        public UserNameException(String msg){
            super(msg);
        }
    }
    public static void main(String[] args) {
        UserLogin userLogin = new UserLogin();
        try{
            userLogin.login("admin","123456");
        }catch(PasswordException e){
            //只需要捕获受查异常即可
            e.printStackTrace();
        }
    }
    public void login(String userName,String password) throws PasswordException{
        if(!this.userName.equals(userName)){
            throw new UserNameException("用户名错误！");
        }
        if(!this.password.equals(password)){
            throw new PasswordException("密码错误！");
        }
    }
}
